package com.itcast.adapter;

import java.util.List;

import com.itcast.R;
import com.itcast.activity.BaseActivity.ImageCallback;
import com.itcast.util.ImageUtil;
import com.itcast.vo.HomeGallery;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class HomeGalleryFiveAdapter extends BaseAdapter {
	private Gallery gallery;
	private List<HomeGallery> galleryList;
	private Context context;
	
	public HomeGalleryFiveAdapter(List<HomeGallery> galleryList,Context context,Gallery gallery){
		this.galleryList = galleryList;
		this.context = context;
		this.gallery = gallery;
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView view;
		if(convertView==null){
			view = new ImageView(context);
		}else{
			view = (ImageView) convertView;
		}
		view.setScaleType(ScaleType.FIT_XY);
//		String imgUrl = galleryList.get(position).pic;
		String imgUrl = "http://192.168.1.247:8080/AndroidServer/images/image1.png";
		String imagePath = ImageUtil.getCacheImgPath().concat(ImageUtil.md5(imgUrl));
		view.setTag(imagePath);
		Bitmap bitmap = ImageUtil.loadImage(imagePath, imgUrl, new ImageCallback() {
			@Override
			public void loadImage(Bitmap bitmap, String imagePath) {
				ImageView imgView = (ImageView) gallery.findViewWithTag(imagePath);
				if(imgView != null){
					imgView.setImageBitmap(bitmap);
				}
			}
		});
		if(bitmap==null){
			view.setImageResource(R.drawable.product_loading);
		}else{
			view.setImageBitmap(bitmap);
		}
		return view;
	}

}
